<?php

namespace App\Model\form;

use App\Constants\ConstCode;
use App\Model\WeworkOperation;
use Hyperf\DbConnection\Db;

trait WeworkOperationTrait
{
    /**
     * 获取运营者详情
     * @param  $where
     * @return array
     */
    public function getWeworkOperationInfo($where): array
    {
        $result = WeworkOperation::where($where)->first();
        return !empty($result) ? $result->toArray() : [];
    }

    /**
     * 任务设备表和虚拟坐席表联合查询
     * @param array $params
     * @return array
     */
    public function getWeworkOperation(array $params): array
    {
        $where = [
            'marketUserId' => $params['marketUserId'],
            'status' => ConstCode::IS_USE,
        ];
        $result = WeworkOperation::where($where)
            ->with(['UserDevice' => function ($query) {
                $query->select('id', 'token');
            }])->first();
        return !empty($result) ? $result->toArray() : [];
    }

    /**
     *获取运营号账号列表
     * @param object $where
     * @return array
     */
    public function getOperationAccountList(object $where): array
    {
        $result = WeworkOperation::where($where)
            ->with(['Account' => function ($query) {
                $query->select('id', 'alias_name', 'real_name', 'account_name', 'company_name', 'avatar', 'sex', 'mobile', 'marketUserId');
            }])
            ->get()->toArray();
        return $result;
    }

    /**
     *获取运营号账号详情
     * @param object $where
     * @return array
     */
    public function getOperationAccount(object $where): array
    {
        $result = WeworkOperation::where($where)
            ->with(['Account' => function ($query) {
                $query->select('id', 'alias_name', 'real_name', 'account_name', 'company_name', 'avatar', 'sex', 'mobile', 'marketUserId');
            }])
            ->first();
        return !empty($result) ? $result->toArray() : [];
    }

    /**
     * 获取运营者数量
     * @param object $where
     * @return array
     */
    public function getWeworkOperationList(object $where): array
    {
        return WeworkOperation::where($where)->get()->toArray();
    }

    /**
     * 获取运营者MarketUserId列表
     * @param  $where
     * @return array
     */
    public function getWeworkOperationMarketUserIdArr($where): array
    {
        return  WeworkOperation::where($where)->pluck('marketUserId')->toArray();
    }


    /**
     * 获取运营者表的设备id
     * @param  $where
     * @return int
     */
    public function getWeworkOperationDeviceId($where): int
    {
        return  WeworkOperation::where($where)->value('device_id');
    }

    /**
     * 获取运营者表的userid
     * @param  $where
     * @return int
     */
    public function getWeworkOperationUserId($where): int
    {
        return  WeworkOperation::where($where)->value('user_id');
    }


}